package com.music.mysongs.activity

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.google.gson.Gson
import com.music.mysongs.web.CommonWebTask
import com.music.mysongs.data.MusicCreatorData
import com.music.mysongs.data.getApiAccountChk
import com.music.mysongs.data.getApiAccountMd5
import com.music.mysongs.data.getApiAccountReg
import com.music.mysongs.helper.myActReadStringSPF
import com.music.mysongs.helper.myActWriteSPF
import com.music.mysongs.utils.BaseActivity
import com.music.mysongs.utils.myGoActivity
import com.music.mysongs.utils.myLog
import com.music.mysongs.utils.myToast
import kotlinx.android.synthetic.main.activity_splash.*

class SplashActivity : BaseActivity() {
    private val mTAG = "SplashActivity"
    private var userExist = false
    private var userPsd = ""
    private var userId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.music.mysongs.R.layout.activity_splash)
        goneThreeBar(this)
        val nameSPF = myActReadStringSPF(this, "spfUserName")
        myLog("$mTAG: nameSPF=$nameSPF")
        if (nameSPF != "none") {
            handDelay(null, null)
        }
        editRegName_splash.setOnFocusChangeListener { _, hasFocus ->
            if (!hasFocus) {
                val name = editRegName_splash.text.toString()
                chkName(name)
            }
        }
        btnLogin_splash.setOnClickListener {
            btnListen(false)
        }
        btnRegister_splash.setOnClickListener {
            btnListen(true)
        }
    }

    private fun btnListen(reg: Boolean) {
        val name = editRegName_splash.text.toString()
        val pwd = editRegPwd_splash.text.toString()
        if (pwd == "") {
            myToast(this, "请输入密码!")
        } else if (reg) {
            chkName(name)
            saveRegister(pwd.toInt(), name)
        } else if (!reg) {
            chkName(name)
            chkPsd(pwd.toInt(), name)
        }
    }

    private fun chkName(name: String) {
        val regTask = CommonWebTask()
        regTask.url = getApiAccountChk(name)
        regTask.txtView = txtRegName_splash
        regTask.setCallbackHandler { innerData ->
            userExist = innerData.contains("20001")
            if (userExist) {
                val data = Gson().fromJson(innerData, MusicCreatorData.AdminData::class.java)
                userPsd = data.data!!.pwd
                userId = data.data.id.toInt()
            } else {
                myLog(innerData)
            }
        }
        regTask.execute()
    }

    private fun chkPsd(psd: Int, name: String) {
        if (userExist) {
            val regTask = CommonWebTask()
            regTask.url = getApiAccountMd5(psd)
            regTask.setCallbackHandler { innerData ->
                val data = Gson().fromJson(innerData, MusicCreatorData.Md5Data::class.java)
                if (userPsd == data.data) {
                    myToast(this, "成功登录 ~")
                    handDelay(userId, name)
                } else {
                    myToast(this, "密码错误 !")
                }
            }
            regTask.execute()
        } else {
            myToast(this, "先注册哦 !")
        }
    }

    private fun saveRegister(pwd: Int, name: String) {
        if (!userExist) {
            val regTask = CommonWebTask()
            regTask.url = getApiAccountReg(pwd, name)
            regTask.setCallbackHandler { innerData ->
                val data = Gson().fromJson(innerData, MusicCreatorData.AdminData::class.java)
                if (data.code == 20001) {
                    myToast(this, "注册成功 ~")
                    handDelay(data.data!!.id.toInt(), data.data.name)
                } else {
                    myToast(this, "注册失败 !")
                    myLog("$mTAG: data=$data")
                }
            }
            regTask.execute()
        }
    }

    //延迟2秒进入主界面
    private fun handDelay(id: Int?, name: String?) {
        if (id != null) {
            //myActWriteSPF(this, "spfPassWord", psd, null)
            myActWriteSPF(this, "spfUserId", null, id)
            myActWriteSPF(this, "spfUserName", name, null)
        }
        txtRegName_splash.visibility = View.GONE
        editRegName_splash.visibility = View.GONE
        editRegPwd_splash.visibility = View.GONE
        btnLogin_splash.visibility = View.GONE
        btnRegister_splash.visibility = View.GONE
        Handler().postDelayed({
            myGoActivity(this@SplashActivity, MainActivity::class.java)
            finish()
        }, 1000 * 2.toLong())
    }

    override fun onPause() {
        super.onPause()
        skyView.pauseAnim()
    }

    override fun onResume() {
        super.onResume()
        skyView.resumeAnim()
    }

    //导航栏状态栏透明不占位
    private fun goneThreeBar(activity: Activity) {
        supportActionBar?.hide()
        if (Build.VERSION.SDK_INT >= 21) {
            val decorView = activity.window.decorView
            val option = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
            decorView.systemUiVisibility = option
            activity.window.navigationBarColor = Color.TRANSPARENT
            activity.window.statusBarColor = Color.TRANSPARENT
        }
    }
}